package com.neuosft.his.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.neuosft.his.entity.CommonResult;

 
public class ToJsonFilter implements Filter {

	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		chain.doFilter(request, response);
		Object object = request.getAttribute(CommonResult.RESULT);
		if(object == null){
			CommonResult result = new CommonResult();
			result.innerException();
			object =  result;
		}
		ObjectMapper mapper = new ObjectMapper();
		String json =  mapper.writeValueAsString(object);
		response.getWriter().write(json);
	}

	public void destroy() {
		// TODO Auto-generated method stub

	}

}
